<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="航线任务" prop="planId">
        <!-- <el-input v-model="queryParams.planId" placeholder="请输入任务Id" clearable @keyup.enter="handleQuery" /> -->
          <el-select v-model="queryParams.planId" placeholder="请选择航线任务" clearable >
            <el-option v-for="item in planList" :key="item.planId" :label="item.planName"
              :value="parseInt(item.planId)"></el-option>
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['manage:planDtl:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['manage:planDtl:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:planDtl:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['manage:planDtl:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planDtlList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" prop="id" />
      <el-table-column label="航线任务" align="center" prop="planId">
        <template #default="scope">
          <div v-for="item in planList" :key="item.planId">
            <span v-if="item.planId == scope.row.planId">{{ item.planName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="航点序号" align="center" prop="planSeq" />
      <el-table-column label="飞行控制" align="center" prop="planCmd">
        <template #default="scope">
          <dict-tag :options="plan_cmd" :value="scope.row.planCmd" />
        </template>
      </el-table-column>
      <el-table-column label="飞行速度" align="center" prop="flySpeed" />
      <el-table-column label="飞行高度" align="center" prop="flyHeight" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:planDtl:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:planDtl:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改任务航点对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="planDtlRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="航线任务" prop="planId">
          <el-select v-model="form.planId" placeholder="请选择航线任务" style="width: 100%">
            <el-option v-for="item in planList" :key="item.planId" :label="item.planName"
              :value="parseInt(item.planId)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="航点序号" prop="planSeq">
          <el-input v-model="form.planSeq" placeholder="请输入航点序号" />
        </el-form-item>
        <el-form-item label="飞行控制" prop="planCmd">
          <el-select v-model="form.planCmd" placeholder="请选择飞行控制">
            <el-option v-for="dict in plan_cmd" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="飞行速度" prop="flySpeed">
          <el-input v-model="form.flySpeed" placeholder="请输入飞行速度" />
        </el-form-item>
        <el-form-item label="飞行高度" prop="flyHeight">
          <el-input v-model="form.flyHeight" placeholder="请输入飞行高度" />
        </el-form-item>
        <el-form-item label="停留时间" prop="holdTimes">
          <el-input v-model="form.holdTimes" placeholder="请输入停留时间秒" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="云台俯仰角" prop="pitchAngel">
          <el-input v-model="form.pitchAngel" placeholder="请输入云台俯仰角" />
        </el-form-item>
        <el-form-item label="云台偏航角" prop="yawAngel">
          <el-input v-model="form.yawAngel" placeholder="请输入云台偏航角" />
        </el-form-item>
        <el-form-item label="相机控制" prop="cameraCmd">
          <el-select v-model="form.cameraCmd" placeholder="请选择相机控制">
            <el-option v-for="dict in camera_cmd" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="控制时长" prop="cameraTimes">
          <el-input v-model="form.cameraTimes" placeholder="请输入控制时长" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PlanDtl">
import { listPlanDtl, getPlanDtl, delPlanDtl, addPlanDtl, updatePlanDtl } from "@/api/manage/planDtl"
import { listPlanHdr } from "@/api/manage/planHdr";
import { loadAllParams } from "@/api/page";


const { proxy } = getCurrentInstance()
const { camera_cmd, plan_cmd } = proxy.useDict('camera_cmd', 'plan_cmd')

const planDtlList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    planId: null,
    planSeq: null,
    planCmd: null,
    cameraCmd: null,
  },
  rules: {
    planId: [
      { required: true, message: "任务Id不能为空", trigger: "blur" }
    ],
    planSeq: [
      { required: true, message: "航点序号不能为空", trigger: "blur" }
    ],
    planCmd: [
      { required: true, message: "飞行控制不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询任务航点列表 */
function getList() {
  loading.value = true
  listPlanDtl(queryParams.value).then(response => {
    planDtlList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    planId: null,
    planSeq: null,
    planCmd: null,
    flySpeed: null,
    flyHeight: null,
    holdTimes: null,
    longitude: null,
    latitude: null,
    pitchAngel: null,
    yawAngel: null,
    cameraCmd: null,
    cameraTimes: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("planDtlRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加任务航点"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPlanDtl(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改任务航点"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["planDtlRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePlanDtl(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPlanDtl(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除任务航点编号为"' + _ids + '"的数据项？').then(function () {
    return delPlanDtl(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/planDtl/export', {
    ...queryParams.value
  }, `planDtl_${new Date().getTime()}.xlsx`)
}

/** 获取航线任务 */
const planList = ref([]);
function getPlanList() {
  listPlanHdr(loadAllParams).then(response => {
    planList.value = response.rows
  })
}

getPlanList()
getList()
</script>
